package wz

import (
	"github.com/go-resty/resty/v2"
	"github.com/zeromicro/go-zero/core/logx"
	"navi-mumbai/common/xerr"
	"strconv"
	"time"
)

type TransferToGameReq struct {
	User     string `json:"user"`
	Uid      string `json:"uid"`      // B端用户ID
	Amount   string `json:"amount"`   // 转入金额
	TradeNo  string `json:"trade_no"` // B端订单号
	Currency string `json:"currency"` // 币种
}

type TransferToGameData struct {
	Username   string `json:"username"`
	BTxId      string `json:"b_tx_id"`
	GTxId      string `json:"g_tx_id"`
	NewBalance string `json:"new_balance"`
	OldBalance string `json:"old_balance"`
	Currency   string `json:"currency"`
}

type TransferToGameResp struct {
	Code int                `json:"code"`
	Data TransferToGameData `json:"data"`
}

const transferToGameURI = "wzgames/wallet/deposit"

func (r *HttpClient) TransferToGame(req *TransferToGameReq) (resp *TransferToGameResp, err error) {
	// md5
	stamp := strconv.FormatInt(time.Now().Unix(), 10)
	sign := md5v(r.Id + req.User + req.Uid + req.Amount + req.TradeNo + req.Currency + stamp + r.Secret)
	resp = &TransferToGameResp{}

	logx.WithDuration(time.Second).Infof("TransferToGame req %#v stamp %s sign %s ", req, stamp, sign)

	var rsp *resty.Response
	rsp, err = r.cli.R().SetHeader("Accept", "application/json").SetQueryParams(map[string]string{
		"operator": r.Id,
		"user":     req.User,
		"uid":      req.Uid,
		"amount":   req.Amount,
		"trade_no": req.TradeNo,
		"currency": req.Currency,
		"stamp":    stamp,
		"sign":     sign,
	}).SetResult(resp).SetError(resp).Post(r.Host + transferToGameURI)
	if err != nil {
		return
	}
	if rsp.IsError() {
		err = xerr.NewGrpcErrCodeMsg(xerr.ThirdPartRequestError, rsp.Status())
		return
	}
	logx.WithDuration(time.Second).Infof("TransferToGame resp %#v ", resp)
	return
}
